package nc.ui.levmpoundmanager.poundbillmanager.action;

import java.awt.event.ActionEvent;
import nc.bs.framework.common.NCLocator;
import nc.itf.levmmeampoundbill.poundbill.IPoundQueryBill;
import nc.itf.levmpoundmanager.poundpushbill.ILevmPoundPushBill;
import nc.ui.levmmeampoundbill.poundbill.action.AbstractPoundAction;
import nc.ui.mmgp.uif2.actions.MMGPRefreshAction;
import nc.ui.uif2.ShowStatusBarMsgUtil;
import nc.ui.uif2.UIState;
import nc.vo.levmmeambill.meambill.CreateTypeEnum;
import nc.vo.levmmeampoundbill.poundbill.AggPoundBillVO;
import nc.vo.levmmeampoundbill.poundbill.PoundBillBVO;
import nc.vo.levmmeampoundbill.poundbill.PoundBillStatusEnum;
import nc.vo.levmmeampoundbill.poundbill.PoundCreateTypeEnum;
import nc.vo.levmmeampoundbill.poundbill.PushBillStatusEnum;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmgp.util.MMArrayUtil;
import nc.vo.mmgp.util.MMStringUtil;
import nc.vo.pub.BusinessException;

public class PoundPushBillAction extends AbstractPoundAction {
  private static final long serialVersionUID = 8962499714820727415L;
  
  private MMGPRefreshAction refreshAction;
  
  public PoundPushBillAction() {
    setBtnName(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0056"));
    setCode("pushBillAction");
  }
  
  public void doAction(ActionEvent e) throws Exception {
    AggPoundBillVO aggVO = (AggPoundBillVO)getModel().getSelectedData();
    if (aggVO == null || MMArrayUtil.isEmpty((Object[])aggVO.getChildrenVO()))
      throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0057")); 
    beforePushBillCheck(aggVO);
    Integer successNum = getLevmPoundPushBill().poundPushBill(new String[] { aggVO
          .getPrimaryKey() });
    AggPoundBillVO newAggVO = getNewAggPoundBill(aggVO.getPrimaryKey());
    if (successNum.intValue() == 1) {
      getModel().directlyUpdate(newAggVO);
      ShowStatusBarMsgUtil.showStatusBarMsg(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0058"), getModel()
          .getContext());
    } else {
      throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0059") + newAggVO
          .getParentVO().getVpushbillinfo());
    } 
  }
  
  private void beforePushBillCheck(AggPoundBillVO aggVO) throws BusinessException {
    if (aggVO.getParentVO().getNbillstatus() == null || aggVO
      .getParentVO().getNbillstatus().intValue() != PoundBillStatusEnum.BILLSTATUS_COMPLETE
      .toIntValue())
      throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0060")); 
    if (PoundCreateTypeEnum.CREATED_BEGIN.value().equals(aggVO
        .getParentVO().getNcreatetype()))
      throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0061")); 
    if (aggVO.getParentVO().getNsrccreatetype() == null || aggVO
      .getParentVO().getNsrccreatetype().intValue() != CreateTypeEnum.SYNCHRONOUS
      .toIntValue())
      throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0062")); 
    if ((aggVO.getParentVO().getBnowreturn() != null && aggVO.getParentVO().getBnowreturn().booleanValue()) || (aggVO.getParentVO().getBcanceldeliver() != null && aggVO.getParentVO().getBcanceldeliver().booleanValue()))
      throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0153")); 
    if (!hasNotPushBillRow(aggVO))
      throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0063")); 
  }
  
  private boolean hasNotPushBillRow(AggPoundBillVO aggVO) {
    PoundBillBVO[] bodyVOs = aggVO.getChildrenVO();
    for (PoundBillBVO bodyVO : bodyVOs) {
    	Integer npushbillstatus=bodyVO.getNpushbillstatus();
      if (npushbillstatus == null ||npushbillstatus.intValue() == PushBillStatusEnum.PUSH_BILL_YET.toIntValue()) {
        return true; 
      }else if(npushbillstatus==PushBillStatusEnum.PUSH_BILL_NOT.toIntValue()) {
    	  return false;
      }
      if (MMStringUtil.isEmpty(bodyVO.getPk_downbill_b())) {
        return true; 
      }
    } 
    return false;
  }
  
  private ILevmPoundPushBill getLevmPoundPushBill() {
    return (ILevmPoundPushBill)NCLocator.getInstance().lookup(ILevmPoundPushBill.class);
  }
  
  private AggPoundBillVO getNewAggPoundBill(String pk) throws BusinessException {
    IPoundQueryBill service = (IPoundQueryBill)NCLocator.getInstance().lookup(IPoundQueryBill.class);
    AggPoundBillVO newAggVO = service.getAggPoundBillVOByPk(pk);
    return newAggVO;
  }
  
  protected boolean isActionEnable() {
    return (
      getModel().getUiState() == UIState.NOT_EDIT && 
      getModel().getSelectedData() != null);
  }
  
  public MMGPRefreshAction getRefreshAction() {
    return this.refreshAction;
  }
  
  public void setRefreshAction(MMGPRefreshAction refreshAction) {
    this.refreshAction = refreshAction;
  }
}
